using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class OutStockLog
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("OutStockLogID", "HL_OutStockLog");
		}
		public bool Exists(int OutStockLogID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HL_OutStockLog");
			stringBuilder.Append(" where OutStockLogID=@OutStockLogID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@OutStockLogID", SqlDbType.Int, 4)
			};
			array[0].Value = OutStockLogID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.OutStockLog model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HL_OutStockLog(");
			stringBuilder.Append("UserID,Email,MobilePhone,ProductID,BookCount,InputDate,EmailStatus,MobileStatus,MessageStatus,NoticeDate,NoticeStatue)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@UserID,@Email,@MobilePhone,@ProductID,@BookCount,@InputDate,@EmailStatus,@MobileStatus,@MessageStatus,@NoticeDate,@NoticeStatue)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@UserID", SqlDbType.Int, 4),
				new SqlParameter("@Email", SqlDbType.NVarChar, 50),
				new SqlParameter("@MobilePhone", SqlDbType.NVarChar, 30),
				new SqlParameter("@ProductID", SqlDbType.Int, 4),
				new SqlParameter("@BookCount", SqlDbType.Int, 4),
				new SqlParameter("@InputDate", SqlDbType.DateTime),
				new SqlParameter("@EmailStatus", SqlDbType.Bit, 1),
				new SqlParameter("@MobileStatus", SqlDbType.Bit, 1),
				new SqlParameter("@MessageStatus", SqlDbType.Bit, 1),
				new SqlParameter("@NoticeDate", SqlDbType.DateTime),
				new SqlParameter("@NoticeStatue", SqlDbType.Int, 4)
			};
			array[0].Value = model.UserID;
			array[1].Value = model.Email;
			array[2].Value = model.MobilePhone;
			array[3].Value = model.ProductID;
			array[4].Value = model.BookCount;
			array[5].Value = model.InputDate;
			array[6].Value = model.EmailStatus;
			array[7].Value = model.MobileStatus;
			array[8].Value = model.MessageStatus;
			array[9].Value = model.NoticeDate;
			array[10].Value = model.NoticeStatue;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.OutStockLog model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_OutStockLog set ");
			stringBuilder.Append("UserID=@UserID");
			if (!string.IsNullOrEmpty(model.Email))
			{
				stringBuilder.Append(",Email='" + model.Email + "'");
			}
			if (!string.IsNullOrEmpty(model.MobilePhone))
			{
				stringBuilder.Append(",MobilePhone='" + model.MobilePhone + "'");
			}
			if (!string.IsNullOrEmpty(model.ProductID.ToString()))
			{
				stringBuilder.Append(",ProductID=" + model.ProductID);
			}
			if (!string.IsNullOrEmpty(model.BookCount.ToString()))
			{
				stringBuilder.Append(",BookCount=" + model.BookCount);
			}
			if (model.InputDate.HasValue)
			{
				stringBuilder.Append(",InputDate='" + model.InputDate + "'");
			}
			if (model.NoticeDate.HasValue)
			{
				stringBuilder.Append(",NoticeDate='" + model.NoticeDate + "'");
			}
			if (!string.IsNullOrEmpty(model.NoticeStatue.ToString()))
			{
				stringBuilder.Append(",NoticeStatue=" + model.NoticeStatue);
			}
			stringBuilder.Append(",EmailStatus=@EmailStatus");
			stringBuilder.Append(",MobileStatus=@MobileStatus");
			stringBuilder.Append(",MessageStatus=@MessageStatus");
			stringBuilder.Append(" where OutStockLogID=@OutStockLogID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@UserID", SqlDbType.Int, 4),
				new SqlParameter("@EmailStatus", SqlDbType.Bit, 1),
				new SqlParameter("@MobileStatus", SqlDbType.Bit, 1),
				new SqlParameter("@MessageStatus", SqlDbType.Bit, 1),
				new SqlParameter("@OutStockLogID", SqlDbType.Int, 4)
			};
			array[0].Value = model.UserID;
			array[1].Value = model.EmailStatus;
			array[2].Value = model.MobileStatus;
			array[3].Value = model.MessageStatus;
			array[4].Value = model.OutStockLogID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int OutStockLogID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_OutStockLog ");
			stringBuilder.Append(" where OutStockLogID=@OutStockLogID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@OutStockLogID", SqlDbType.Int, 4)
			};
			array[0].Value = OutStockLogID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string OutStockLogIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_OutStockLog ");
			stringBuilder.Append(" where OutStockLogID in (" + OutStockLogIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.OutStockLog GetModel(int OutStockLogID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HL_OutStockLog ");
			stringBuilder.Append(" where OutStockLogID=@OutStockLogID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@OutStockLogID", SqlDbType.Int, 4)
			};
			array[0].Value = OutStockLogID;
			HKB.Data.Model.OutStockLog outStockLog = new HKB.Data.Model.OutStockLog();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.OutStockLog result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.OutStockLog DataRowToModel(DataRow row)
		{
			HKB.Data.Model.OutStockLog outStockLog = new HKB.Data.Model.OutStockLog();
			if (row != null)
			{
				if (row["OutStockLogID"] != null && row["OutStockLogID"].ToString() != "")
				{
					outStockLog.OutStockLogID = int.Parse(row["OutStockLogID"].ToString());
				}
				if (row["UserID"] != null && row["UserID"].ToString() != "")
				{
					outStockLog.UserID = new int?(int.Parse(row["UserID"].ToString()));
				}
				if (row["Email"] != null)
				{
					outStockLog.Email = row["Email"].ToString();
				}
				if (row["MobilePhone"] != null)
				{
					outStockLog.MobilePhone = row["MobilePhone"].ToString();
				}
				if (row["ProductID"] != null && row["ProductID"].ToString() != "")
				{
					outStockLog.ProductID = new int?(int.Parse(row["ProductID"].ToString()));
				}
				if (row["BookCount"] != null && row["BookCount"].ToString() != "")
				{
					outStockLog.BookCount = new int?(int.Parse(row["BookCount"].ToString()));
				}
				if (row["InputDate"] != null && row["InputDate"].ToString() != "")
				{
					outStockLog.InputDate = new System.DateTime?(System.DateTime.Parse(row["InputDate"].ToString()));
				}
				if (row["EmailStatus"] != null && row["EmailStatus"].ToString() != "")
				{
					if (row["EmailStatus"].ToString() == "1" || row["EmailStatus"].ToString().ToLower() == "true")
					{
						outStockLog.EmailStatus = true;
					}
					else
					{
						outStockLog.EmailStatus = false;
					}
				}
				if (row["MobileStatus"] != null && row["MobileStatus"].ToString() != "")
				{
					if (row["MobileStatus"].ToString() == "1" || row["MobileStatus"].ToString().ToLower() == "true")
					{
						outStockLog.MobileStatus = true;
					}
					else
					{
						outStockLog.MobileStatus = false;
					}
				}
				if (row["MessageStatus"] != null && row["MessageStatus"].ToString() != "")
				{
					if (row["MessageStatus"].ToString() == "1" || row["MessageStatus"].ToString().ToLower() == "true")
					{
						outStockLog.MessageStatus = true;
					}
					else
					{
						outStockLog.MessageStatus = false;
					}
				}
				if (row["NoticeDate"] != null && row["NoticeDate"].ToString() != "")
				{
					outStockLog.NoticeDate = new System.DateTime?(System.DateTime.Parse(row["NoticeDate"].ToString()));
				}
				if (row["NoticeStatue"] != System.DBNull.Value)
				{
					outStockLog.NoticeStatue = System.Convert.ToInt32(row["NoticeStatue"].ToString());
				}
			}
			return outStockLog;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * FROM HL_OutStockLog ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * FROM HL_OutStockLog ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HL_OutStockLog ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.OutStockLogID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HL_OutStockLog T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
